<!-- 游戏编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate ? '修改游戏' : '添加游戏'"
    :visible="visible"
    width="1000px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="150px">
      <el-form-item label="关联项目:" prop="app_id">
        <PageSelect
          :placeholder="'请选择关联项目'"
          v-model="form.app_id"
          :whereName="'app_name'"
          :getAllurl="'/app/getAll'"
          :url="'/app/index'"
          :optionLable="'app_name'"
          :optionvalue="'id'"
          :multiple="false"
          :disabled="isUpdate"
        />
      </el-form-item>

      <el-form-item label="发货接口地址:" prop="pay_notify_url">
        <el-input
          v-model="form.pay_notify_url"
          placeholder="请输入发货接口地址"
          clearable
        />
      </el-form-item>
        <!-- <PageSelect
          :placeholder="'请输入推广渠道'"
          v-model="form.promote_id"
          :whereName="'app_name'"
          :getAllurl="'/app/getAll'"
          :url="'/app/index'"
          :optionLable="'app_name'"
          :optionvalue="'id'"
          :multiple="false"
        /> -->
        <!-- <el-select v-model="form.channel_id">
          <el-option label="自有渠道" :value="0"></el-option>
        </el-select>
      </el-form-item> -->

      <el-form-item label="游戏名称:" prop="game_name">
        <el-input
          v-model="form.game_name"
          placeholder="请输入游戏名称"
          clearable
        />
      </el-form-item>
      <el-form-item
        label="研发游戏ID:"
      >
        <el-input
          :maxlength="32"
          v-model="form.yanfa_game_id"
          placeholder="请输入研发游戏ID"
          clearable
        />
      </el-form-item>
      <!-- <el-form-item label="默认推广员:" prop="game_name">
        <PageSelect
          :placeholder="'请选择推广员'"
          v-model="form.admin_id"
          :whereName="'app_name'"
          :url="'/userpromote/index'"
          :optionLable="'realname'"
          :optionvalue="'id'"
          :multiple="false"
        />
      </el-form-item> -->

      <!-- <el-form-item label="icon:" prop="icon"> -->
        <!-- <el-input
          :maxlength="20"
          v-model="form.icon"
          placeholder="请输入icon"
          clearable
        /> -->
        <!-- <uploadImage :limit="1" v-model="form.icon"></uploadImage>
      </el-form-item> -->

      <!-- <el-form-item label="区别版本" prop="sdk_version"> -->
        <!-- <el-input-number
          :min="0"
          v-model="form.sdk_version"
          placeholder="请输入区别版本   1安卓 2苹果"
          controls-position="right"
          class="ele-fluid ele-text-left"
        /> -->
        <!-- <el-radio-group :disabled="isUpdate" v-model="form.sdk_version">
          <el-radio :label="1">安卓</el-radio>
          <el-radio :label="2">苹果</el-radio>
        </el-radio-group>
      </el-form-item> -->

      <!-- <el-form-item label="游戏appid:" prop="game_appid">
        <el-input
          v-model="form.game_appid"
          placeholder="请输入游戏appid"
          clearable
        />
      </el-form-item> -->

      <!-- <el-form-item
        v-if="form.channel_id !== 0"
        label="游戏支付通知地址:"
        prop="pay_notify_url"
      >
        <el-input
          v-model="form.pay_notify_url"
          placeholder="请输入游戏支付通知地址"
          clearable
        />
      </el-form-item>

      <el-form-item label="访问秘钥:" prop="access_key">
        <el-input
          v-model="form.access_key"
          placeholder="请输入访问秘钥"
          clearable
        />
      </el-form-item>

      <el-form-item label="状态::" prop="status"> -->
        <!-- <el-input-number
          :min="0"
          v-model="form.status"
          placeholder="请输入状态:1=启用 0=禁用"
          controls-position="right"
          class="ele-fluid ele-text-left"
        /> -->
        <!-- <el-radio-group v-model="form.status">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item> -->
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" @click="save" :loading="loading"
        >保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
// import uploadImage from '@/components/uploadImage'
import PageSelect from '@/components/pageSelect/index.vue'

export default {
  name: 'GameEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: { PageSelect },
  data() {
    return {
      // 表单数据
      form: Object.assign({ status: 1 }, this.data),
      where: {},
      // 表单验证规则
      rules: {
        app_id: [
          { required: true, message: '请输入关联项目', trigger: 'blur' }
        ],
        channel_id: [
          { required: true, message: '请输入推广渠道', trigger: 'blur' }
        ],
        game_name: [
          { required: true, message: '请输入游戏名称', trigger: 'blur' }
        ],
        // icon: [{ required: true, message: '请输入icon', trigger: 'blur' }],
        sdk_version: [
          {
            required: true,
            message: '请输入区别版本',
            trigger: 'blur'
          }
        ],
        game_appid: [
          { required: true, message: '请输入游戏appid', trigger: 'blur' }
        ],
        relation_game_id: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        relation_game_name: [
          { required: true, message: '请输入游戏名称', trigger: 'blur' }
        ],
        // pay_notify_url: [
        //   { required: true, message: '请输入游戏支付通知地址', trigger: 'blur' }
        // ],
        access_key: [
          { required: true, message: '请输入访问秘钥', trigger: 'blur' }
        ],
        status: [
          {
            required: true,
            message: '请输入状态',
            trigger: 'blur'
          }
        ]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    }
  },
  watch: {
    // 'form.channel_id'(val) {
    //   if (val === 0) {
    //     this.$delete(this.form, 'pay_notify_url')
    //   } else {
    //     this.$set(this.form, 'pay_notify_url', '')
    //   }
    // },
    visible(val) {
      if (val) {
        if (this.data) {
          this.form = Object.assign({}, this.data)
          this.isUpdate = true
        } else {
          this.form = {}
          this.isUpdate = false
        }
      }
    }
  },
  methods: {
    /* 保存编辑 */
    // changeAppid(val) {
    //   this.$set(this.form, 'app_id', val)
    //   console.log(this.form)
    // },
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.$http
            .post('/game/edit', this.form)
            .then((res) => {
              this.loading = false
              if (res.data.code === 0) {
                this.$message.success(res.data.msg)
                if (!this.isUpdate) {
                  this.form = {}
                }
                this.updateVisible(false)
                this.$emit('done')
              } else {
                this.$message.error(res.data.msg)
              }
            })
            .catch((e) => {
              this.loading = false
              this.$message.error(e.message)
            })
        } else {
          return false
        }
      })
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value)
    }
  }
}
</script>

<style scoped>
</style>
